package com.xuecheng.content.controller;

import com.xuecheng.commons.model.dto.CourseAuditDto;
import com.xuecheng.commons.model.dto.CourseBaseDto;
import com.xuecheng.commons.model.dto.CourseDto;
import com.xuecheng.commons.model.dto.PageRequest;
import com.xuecheng.commons.model.vo.CourseBaseVo;
import com.xuecheng.commons.model.vo.CourseVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.content.domain.CourseBase;
import com.xuecheng.content.service.CourseBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 课程基本信息前端控制器
 */
@Slf4j
@RestController
@RequestMapping("/course")
public class CourseBaseController {

    @Autowired
    private CourseBaseService  courseBaseService;

    /**
     * 请求路径：POST  /list
     * 请求参数：
     *     Query：分页请求对象pageNo，pageSize  -->PageRequest
     *     Body：分页查询的条件参数 CourseDTO
     * 返回值：
     *    PageResponseResult
     */
    @PostMapping("/list")
    public ResponseResult list(@RequestBody CourseDto dto, PageRequest request) {
        return courseBaseService.findAll(dto,request);
    }

    /**
     * 上传课程封面图片
     * 请求地址： POST /upload
     * 请求参数：uploadFile （文件）
     * 响应内容：ResponseResult<String>
     */
    @PostMapping("/upload")
    public ResponseResult<String> upload(MultipartFile uploadFile) throws Exception {
        return courseBaseService.upload(uploadFile);
    }


    /**
     * 保存课程
     * 请求地址：POST
     * 请求参数：CourseBaseDto
     * 返回值：ResponseResult<CourseBaseVo>
     */
    @PostMapping
    public ResponseResult<CourseBaseVo> saveCourseBase(@RequestBody CourseBaseDto dto) {
        return courseBaseService.saveCourseBase(dto);
    }

    /**
     * 删除: /course/{courseId}
     */
    @DeleteMapping("/{courseId}")
    public ResponseResult deleteById(@PathVariable("courseId") Long courseId) {
        //判断
        courseBaseService.removeById(courseId);//删除
        return ResponseResult.okResult();
    }

    /**
     * 根据id查询课程信息
     * 请求：GET /{courseBaseId}
     * 参数：地址参数，id
     * 响应ResponseResult<CourseVo>
     */
    @GetMapping("/{courseBaseId}")
    public ResponseResult<CourseVo> findBycourseId(@PathVariable("courseBaseId") Long courseBaseId) {
        CourseBase courseBase = courseBaseService.getById(courseBaseId);
        CourseVo vo = BeanHelper.copyProperties(courseBase, CourseVo.class);
        vo.setName(courseBase.getName());
        return ResponseResult.okResult(vo);
    }

    /**
     * 根据id更新课程
     * 地址： PUT
     * 请求参数：CourseBaseDto
     * 响应：ResponseResult<课程ID>
     */
    @PutMapping
    public ResponseResult<CourseBaseVo> updateCourse(@RequestBody CourseBaseDto dto) {
        return courseBaseService.updateCourse(dto);
    }

    /**
     * 机构提交审核
     *   GET :  /commit/{courseBaseId}
     *   参数：课程id
     *   返回：ResponseResult
     */
    @GetMapping("/commit/{courseBaseId}")
    public ResponseResult commit(@PathVariable("courseBaseId") Long courseBaseId) {
        return courseBaseService.commit(courseBaseId);
    }

    /**
     * 运营平台审核课程
     * POST: /approve
     * 参数：CourseAuditDto
     * 响应：ResponseResult
     */
    @PostMapping("/approve")
    public ResponseResult approve(@RequestBody CourseAuditDto dto) {
        return courseBaseService.approve(dto);
    }

}
